//
//  WithdrawSecondSendMsgCell.m
//  GasBusiness
//
//  Created by zfc on 2021/8/1.
//

#import "WithdrawSecondSendMsgCell.h"

@interface WithdrawSecondSendMsgCell ()
@property (nonatomic, strong)dispatch_source_t timer;

@end

@implementation WithdrawSecondSendMsgCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setUI {
    self.contentView.backgroundColor = ColorF5;
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = WhiteColor;
    [self.contentView addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.contentView);
        make.leading.equalTo(self.contentView);
        make.top.equalTo(self.contentView);
    }];

    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"短信验证码";
    lab.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
    lab.textColor = Color333;
    [backView addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(backView).offset(15);
        make.centerY.equalTo(backView);
        make.top.equalTo(backView).offset(25);
    }];

    UITextField *codeTf = [[UITextField alloc] init];
    NSAttributedString *codeAttr = [[NSAttributedString alloc] initWithString:@"请输入发到您手机的验证码" attributes:
    @{NSForegroundColorAttributeName:Color999,
      NSFontAttributeName:[UIFont systemFontOfSize:12 weight:UIFontWeightBold]
    }];
    codeTf.attributedPlaceholder = codeAttr;
    codeTf.text = @"";
    codeTf.keyboardType = UIKeyboardTypeNumberPad;
    [backView addSubview:codeTf];
    self.codeTf = codeTf;
    [codeTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(backView).offset(94);
        make.centerY.equalTo(backView);
        make.trailing.equalTo(backView).offset(-90);
        make.height.mas_equalTo(45);
    }];


    UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [codeBtn setTitle:@"  发送验证码  " forState:UIControlStateNormal];
    [codeBtn setTitleColor:[UIColor colorWithHexString:@"3D6DCC"] forState:UIControlStateNormal];
    codeBtn.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
    [backView addSubview:codeBtn];
    [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(28);
        make.centerY.equalTo(backView);
        make.trailing.equalTo(backView).offset(-15);
    }];

    WeakSelf;
    codeBtn.block = ^(UIButton *sender) {
        //点击发送验证码
        [weakSelf actionGetCode:sender];

    };
}

- (void)setDic:(NSDictionary *)dic {
    _dic = dic;

}

- (void)actionGetCode:(id)sender {
    BOOL isValid = [Untils cheackPhoneNumValid:self.dic[@"mobile"]];
    if(!isValid){
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号码"];
        return;
    }

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"mobile"] = self.dic[@"mobile"];
    params[@"usage"] = @"login";

    [[NetWorksTools shared] requestWithNotTokenMethod:POST url:@"/tiny-shop/v1/site/sms-code" params:params successBlock:^(NSDictionary * _Nonnull response) {

        NSLog(@"登录验证码: %@", response[@"data"]);
        //倒计时状态下的颜色
        [self setTheCountdownButton:sender startWithTime:59 title:@"重新获取"countDownTitle:@"s"mainColor:WhiteColor countColor:WhiteColor countTitleColor:Color999];
    } failureBlock:^(NSError * _Nonnull error) {

    }];
}


#pragma mark - button倒计时
- (void)setTheCountdownButton:(UIButton *)button startWithTime:(NSInteger)timeLine title:(NSString *)title countDownTitle:(NSString *)subTitle mainColor:(UIColor *)mColor countColor:(UIColor *)color countTitleColor:(UIColor *)titleColor{
    //倒计时时间
    NSInteger endTime = [[NSDate date] timeIntervalSince1970] + timeLine;
    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0, 0, queue);
    //每秒执行一次
    dispatch_source_set_timer( self.timer,dispatch_walltime(NULL,0), 1.0 * NSEC_PER_SEC,0);
    WeakSelf;
    dispatch_source_set_event_handler( self.timer, ^{
        NSInteger currentTIme = [[NSDate date] timeIntervalSince1970];

        //倒计时结束，关闭
        if (endTime <= currentTIme) {
            dispatch_source_cancel( weakSelf.timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                button.backgroundColor = mColor;
                [button setTitle:[NSString stringWithFormat:@"  %@  ", title] forState:UIControlStateNormal];
                button.userInteractionEnabled =YES;
            });
        } else {
            NSString *timeStr = [NSString stringWithFormat:@"  %@(%d%@)  ", title,endTime-currentTIme, subTitle];
            dispatch_async(dispatch_get_main_queue(), ^{
                button.backgroundColor = color;
                [button setTitleColor:titleColor forState:UIControlStateNormal];
                [button setTitle:timeStr forState:UIControlStateNormal];
                button.userInteractionEnabled = NO;
            });
        }
    });
    dispatch_resume( self.timer);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
